<?php
header ( 'Content-Type: text/html; charset=utf-8' );

// http://webelop.com.ar/ajax/comunicacion-ajax-php-con-json/
// Javascript a PHP:
// encodeURIComponent() –> variables POST –> rawurldecode() –> utf8_decode() –> MySQL

$mysqli = new mysqli ( "localhost", "sabana", "sabana", "sabana" );

/* check connection */
if ($mysqli->connect_errno) {
	printf ( "Connect failed: %s\n", $mysqli->connect_error );
	exit ();
}

$p = utf8_decode ( rawurldecode ( $_POST ["prueba"] ) );
// $p=utf8_decode(rawurldecode(0));

$query = "SELECT user,email FROM t_usuarios WHERE TIPO>=" . $p . ";";

$result = mysqli_query ( $mysqli, $query );

echo "<pre>";

$field = $mysqli->field_count;
echo "field: " . $field;
echo "<br />";
$count = $mysqli->affected_rows;
echo "count: " . $count;

echo "</pre>";
function mysql_query_fetch_1_ROW_ASSOC($r) {
	while ( $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ) {
		ECHO "<pre>";
		print_r ( $row );
		echo "</pre>";
	}
}
function mysql_query_fetch_1_ROW_NUM($r) {
	while ( $row = mysqli_fetch_array ( $r, MYSQLI_NUM ) ) {
		ECHO "<pre>";
		print_r ( $row );
		echo "</pre>";
	}
}
function mysql_query_fetch_1_ROW_ASSOC_JSON($r) {
	while ( $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ) {
		ECHO "<pre>";
		ECHO json_encode ( $row );
		echo "</pre>";
	}
}
function mysql_query_fetch_1_ROW_ASSOC_JSON_utf8($r, $f, $n) {
	while ( $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ) {
		foreach ( $row as $key => $value ) {
			$arr [$key] = rawurlencode ( utf8_encode ( $value ) );
		}
		
		$main_arr [] = $arr;
	}
	echo "<pre>";
	echo json_encode ( $main_arr );
	echo "</pre>";
}

mysql_query_fetch_1_ROW_ASSOC ( $result );
// mysql_query_fetch_1_ROW_NUM($result);
// mysql_query_fetch_1_ROW_ASSOC_JSON($result);
// mysql_query_fetch_1_ROW_ASSOC_JSON_utf8($result,$field, $count);
function mysql_query_fetch_n_ROWS_ASSOC_array($r, $n) {
	$row = array ();
	for($i = 0; $i < $n; $i ++) {
		$row [] = mysqli_fetch_array ( $r, MYSQLI_ASSOC );
	}
	
	ECHO "<pre>";
	print_r ( $row );
	echo "</pre>";
}
function mysql_query_fetch_n_ROWS_NUM_array($r, $n) {
	$row = array ();
	for($i = 0; $i < $n; $i ++) {
		$row [] = mysqli_fetch_array ( $r, MYSQLI_NUM );
	}
	
	ECHO "<pre>";
	print_r ( $row );
	echo "</pre>";
}
function mysql_query_fetch_n_ROWS_ASSOC_JSON_array($r, $n) {
	$row = array ();
	for($i = 0; $i < $n; $i ++) {
		$row [] = mysqli_fetch_array ( $r, MYSQLI_ASSOC );
	}
	
	ECHO "<pre>";
	ECHO json_encode ( $row );
	echo "</pre>";
}
function mysql_query_fetch_n_ROW_ASSOC_JSON_utf8($r, $f, $n) {
	while ( $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ) {
		foreach ( $row as $key => $value ) {
			$arr [$key] = rawurlencode ( utf8_encode ( $value ) );
		}
		
		$main_arr [] = $arr;
	}
	echo "<pre>";
	echo json_encode ( $main_arr );
	echo "</pre>";
}

// mysql_query_fetch_n_ROWS_ASSOC_array($result,$count);
// mysql_query_fetch_n_ROWS_NUM_array($result,$count);
// mysql_query_fetch_n_ROWS_ASSOC_JSON_array($result,$count);
// mysql_query_fetch_n_ROW_ASSOC_JSON_utf8($result,$field, $count);

mysqli_free_result ( $result );
mysqli_close ( $mysqli );

//Hacia javascript
//objetoNuevo = eval('(' + http2.responseText + ')');
//objetoNuevo.items[i].nombreColumna1;

//http://webelop.com.ar/ajax/comunicacion-ajax-php-con-json/
//PHP a Javascript:
//MySQL –> utf8_encode() –> rawurlencode() –> JSON
/*
<script language="javascript" type="text/javascript">
  var json = eval ( {"poblacion":[ { "id": "0", "nombre": "Alcobendas" },{ "id": "1", "nombre": "Miraflores de la Sierra" }]} );
  if(json.poblacion.length > 0 )
  {
    for (i=0; i < json.poblacion.length; i++)
    {
      alert(json.poblacion[i].nombre);
    }
  }
</script>*/
